<?php

/***
 * Class CW_ConfigClass contains all methods necessary
 * to retrieve, edit and create application settings:
 ***/
class CW_ConfigClass {

   /**
	* All loaded configuration values.
	* @access Private
	* @var Array
	*/
	public $config;


   // ! Constructor Method

   /**
	* Instantiates class and defines instance variables.
	*
	* @param Object $Error Core error logging and reporting library
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access public
	* @return Void
	*/
	public function __construct()
	{
		$this->config = array();
	}


   // ! Accessor Method

   /**
	* Retrieves a setting item from a specified
	* group / child combination.
	*
	* @param String $child Child group to search.
	* @param String $item Name of the specific item to fetch
	* @param String $group Main group to fetch from
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Mixed
	*/
	public function get($child, $item, $group = 'system')
	{
		if(false == $this->config)
		{
			throw new CoreException('There are no configuration items loaded! Load some configuration items first ...');
		}

		if(false == isset($this->config[$group]))
		{
			throw new CoreException("There are no configuration groups or items under '{$group}'!");
		}
		else
		{
			if(false == isset($this->config[$group][$child]))
			{
				throw new CoreException("Configuration child '{$child}' could not be located under '{$group}'!");
			}
			else
			{
				if(false == isset($this->config[$group][$child][$item]))
				{
					throw new CoreException("The configuration item '{$item}' could not be located under '{$child}' and '{$group}'!");
				}
			}
		}

		return $this->config[$group][$child][$item];
	}


   // ! Mutator Method

   /**
	* Allows the system to modify or create settings.
	*
	* @param String $child Child group to search.
	* @param String $item Name of the specific item to fetch
	* @param Mixed $value The value of the new / edited configuration item
	* @param Bool $overwrite Error if TRUE and attempting to modify an existing setting
	* @param String $group Main group to fetch from
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function set($child, $item, $value, $overwrite = false, $group = 'system')
	{
		if(false == $this->config && false == $overwrite)
		{
			throw new CoreException('There are no configuration items loaded! Load some configuration items first ...');
		}

		if(isset($this->config[$child]) && false == $overwrite)
		{
			throw new CoreException("A child by the name of '{$child}' already exists!");
		}
		else if(isset($this->config[$group][$child][$item]) && false == $overwrite)
		{
			throw new CoreException("A configuration item by the name of '{$item}' is already assigned to child '{$child}' in group '{$group}'!");
		}

		$this->config[$group][$child][$item] = $value;

		return true;
	}


   // ! Accessor Method

   /**
	* Loads the requested subroutine, if exists and
	* begins to process it.
	*
	* @param String $path_to_config Absolute path to the configuration file
	* @param String $group Main group to fetch from
	* @param Bool $overwrite Error if TRUE and attempting to modify an existing setting group
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function load($path_to_config, $group, $overwrite = false)
	{
		if(false == file_exists($path_to_config))
		{
			throw new CoreException("Attempted to load configuration file from '{$path_to_config}' and failed! File does not exist.");
		}

		if(in_array($path_to_config, get_required_files()))
		{
			throw new CoreException("Attempted to load configuration file from '{$path_to_config}' and failed! File is already loaded.");
		}

		require_once $path_to_config;

		if(false == $config)
		{
			throw new CoreException("The configuration file '{$path_to_config}' is empty!");
		}

		if(isset($this->config[$group]) && false == $overwrite)
		{
			throw new CoreException("Configuration settings under group '{$group}' are already loaded and cannot be overwritten.");
		}

		if(false == $this->config || false == isset($this->config[$group]))
		{
			$this->config[$group] = $config;
		}
		else if(false == $application && false == $this->config)
		{
			$this->config = $config;
		}

		return true;
	}
}

?>